package zzps.exeed.kernel.holder;

import zzps.exeed.kernel.tool.AnnotationTool;

import java.lang.annotation.Annotation;
import java.lang.reflect.Parameter;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

public class ParameterHolder {

    private Parameter parameter;
    //参数名
    private String name;
    private ParameterizedType parameterizedType;
    //参数类型
    private Class<?> clazz;
    private Object value;
    private Annotation[] annotations;
    //参数如果是组合结构类型，相关的参数数据，在web模块时有用
    private ModelAttribute modelAttribute;


    public ParameterHolder(Parameter parameter) {
        this.parameter = parameter;
        clazz = parameter.getType();
        name = parameter.getName();
        annotations = AnnotationTool.getAnnotations(parameter);
        Type tmp = parameter.getParameterizedType();
        if (tmp instanceof ParameterizedType) {
            parameterizedType = (ParameterizedType) tmp;
        } else {
            parameterizedType = null;
        }
    }

    public Annotation[] getAnnotations() {
        return annotations;
    }

    public void setValue(Object value) {
        this.value = value;
    }

    public Object getValue() {
        return value;
    }

    public Class<?> getType() {
        return clazz;
    }

    public <A extends Annotation> A getParameterAnnotation(Class<A> annotationType){
        for (Annotation annotation : annotations) {
            if (annotationType.isInstance(annotation)) {
                return (A) annotation;
            }
        }
        return null;
    }
    public <A extends Annotation> boolean hasParameterAnnotation(Class<A> annotationType){
        return (getParameterAnnotation(annotationType) !=null);
    }
    public String getName(){
        return name;
    }

    public ModelAttribute getModelAttribute() {
        return modelAttribute;
    }

    public void setModelAttribute(ModelAttribute modelAttribute) {
        this.modelAttribute = modelAttribute;
    }
}
